//
//  SceneViewController.m
//  HappyToday
//
//  Created by Ale (ziming) Lee on 16/7/26.
//  Copyright © 2016年 ziming_lee. All rights reserved.
//

#import "SceneViewController.h"
#import "SceneTableViewCell.h"
#import "UserModel.h"
#import "storyModel.h"


@interface SceneViewController ()<UITableViewDataSource,UITableViewDelegate,PullTableViewDelegate>{

    SegmentView *segmentView;

    NSDictionary *_rootDataDic;//当前城市根数据结构

    //存储Model的数组
    NSMutableArray *_userData;
    NSMutableArray *_storyData;
    NSMutableArray *_praiseData;
    NSMutableArray *_commentData;
}

@property (weak, nonatomic) IBOutlet PullTableView *sceneTableView;

@end

@implementation SceneViewController

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.tabBarItem.image = [UIImage imageNamed:@"icon_tab2_normal@2x"];

//        [self _createSegmentView];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self _createSegmentView];
    [self _loadData];


}
-(void)_loadData{


    if (self.tableType == currentCityTableView) {
        _rootDataDic = [DataParser jsonObjectFromFileName:@"SceneJSONDirections"];

    }
    if (self.tableType == allTableView){
        _rootDataDic = [DataParser jsonObjectFromFileName:@"AllCityJSON"];

    }
    NSDictionary *objcDic = [_rootDataDic objectForKey:@"obj"];
    NSArray *userList = [objcDic objectForKey:@"userList"];
    NSArray *storyList = [objcDic objectForKey:@"storyList"];
//    NSArray *praiseInfoList = [objcDic objectForKey:@"praiseInfoList"];
//    NSArray *commentList = [objcDic objectForKey:@"commentList"];


#pragma ---mark----存储Model的数组-------------------
    _userData = [[NSMutableArray alloc]initWithCapacity:1];
    _storyData = [[NSMutableArray alloc]initWithCapacity:1];
#pragma ---mark----存储Model的数组-------------------

    for (NSDictionary *dic in userList) {

        UserModel *userModel = [[UserModel alloc]initWithDic:dic];
        [_userData addObject:userModel];


    }
    for (NSDictionary *dic in storyList) {
        
        storyModel *story = [[storyModel alloc]initWithDic:dic];
        [_storyData addObject:story];

    }

}
-(void)_createSegmentView{

   segmentView  = [[SegmentView alloc]initWithFrame:CGRectMake(0,60, KScreenW,40) withTitles:@[@"本地",@"全国"] segmentBlock:^(int index) {

        switch (index) {
            case 0:

                self.tableType = currentCityTableView;

                break;
            case 1:

                self.tableType = allTableView;
                break;
                
            default:
                break;
        }

       [self _loadData];
       
       [self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:YES];

    }];

    _sceneTableView.tableHeaderView = segmentView;

}

-(void)reloadTableView{

    [_sceneTableView reloadData];

}
#pragma mark------------tableViewDelegate---------------------------
- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView{
    NSLog(@"下拉刷新");

}
- (void)pullTableViewDidTriggerLoadMore:(PullTableView*)pullTableView{

        NSLog(@"上啦加载");

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (_storyData.count <= _userData.count) {

        return _storyData.count;
        
    }else{


        return _userData.count;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    SceneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"scene_cell"];
    if (!cell) {

        cell = [[[NSBundle mainBundle]loadNibNamed:@"SceneTableViewCell" owner:nil options:nil] lastObject];
    }

    UserModel *userModel = [_userData objectAtIndex:indexPath.row];
    cell.userModel = userModel;
    storyModel *story = [_storyData objectAtIndex:indexPath.row];
    cell.storyModel = story;

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat{

    return 530;
}
#pragma mark------------tableViewDelegate---------------------------
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
